package com.emuporium.gymrat.android;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

import com.emuporium.gymrat.android.data.Routine;
import com.emuporium.gymrat.android.database.DatabaseProgramAdapter;

public class RoutinesMain extends Activity {
	/** Called when the activity is first created. */
	SQLiteDatabase db;
	ListView list;
	DatabaseProgramAdapter adapter = null;

	static int DISPLAY_ROUTINE_REQUEST = 1;
	static final private int MENU_RENAME = 10;
	static final private int MENU_EDIT = 11;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.e("ROUTINEMAIN","CREATE");
		setContentView(R.layout.routines_main);

		Button searchButton = (Button) findViewById(R.id.button_search_routines);
		Button createButton = (Button) findViewById(R.id.button_create_routine);
		Button manageButton = (Button) findViewById(R.id.button_manage_routines);

		list = (ListView) findViewById(R.id.list_routines);
		registerForContextMenu(list);

		db = DbUtils.getStaticDb();
		adapter = new DatabaseProgramAdapter(this);
		adapter.open();
		Cursor c2 = adapter.getAll();
		c2.moveToFirst();
		Log.e("Testing new DB:",c2.getString(c2.getColumnIndex("name")));

		Cursor c = db.rawQuery("select _id, name, description, author from routines", null);
		startManagingCursor(c);
		final ArrayList<Routine> routineList = new ArrayList<Routine>();

		final ArrayAdapter<Routine> aa;
		aa = new ArrayAdapter<Routine>(this,
				android.R.layout.simple_list_item_1, routineList);

		list.setAdapter(aa);

		while (c.moveToNext()) {
			Routine r = new Routine();
			r.setId(c.getInt(0));
			r.setName(c.getString(1));
			r.setDescription(c.getString(2));
			r.setAuthor(c.getString(3));

			routineList.add(r);
		}

		list.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView av, View v, int pos, long id) {
				Intent intent = new Intent(RoutinesMain.this, ShowRoutine.class);
				intent.putExtra("Routine", routineList.get(pos));
				startActivityForResult(intent, DISPLAY_ROUTINE_REQUEST);

				// setResult(RESULT_OK, i);
				// finish();
			}
		});

		searchButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Intent intent = new Intent(GymRat.this, RoutinesMain.class);
				// startActivity(intent);
			}
		});

		createButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(RoutinesMain.this,
						RoutinesCreate.class);
				startActivity(intent);
			}
		});

		manageButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Intent intent = new Intent(GymRat.this, RoutinesMain.class);
				// startActivity(intent);
			}
		});

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Context Menu");
		menu.add(0, MENU_RENAME, Menu.NONE, "Rename");

		// Intent gotoEdit = new
		// Intent(RoutinesMain.this,RoutinesListExercises.class);
		menu.add(0, MENU_EDIT, Menu.NONE, "Edit Exercises");
		// menu.add(0, menu.FIRST+1, Menu.NONE, "Item 2").setCheckable(true);
		// menu.add(0, menu.FIRST+2, Menu.NONE, "Item 3");
		// SubMenu sub = menu.addSubMenu("Submenu");
		// sub.add("Submenu Item");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
				.getMenuInfo();

		Intent intent;
		Routine r;

		switch (item.getItemId()) {
		case (MENU_RENAME):
			intent = new Intent(RoutinesMain.this, RoutinesCreate.class);
			r = (Routine) list.getAdapter().getItem(menuInfo.position);
			intent
					.putExtra("com.emuporium.gymrat.android.RoutineId", r
							.getId());
			intent.putExtra("Routine", r);
			intent.putExtra("Source", "rename");
			startActivityForResult(intent, 11);
			return true;
		case (MENU_EDIT):
			intent = new Intent(RoutinesMain.this, RoutinesListExercises.class);
			// Favorite favContexted = (Favorite) mFavList.getAdapter().getItem(
			// menuInfo.position);
			r = (Routine) list.getAdapter().getItem(menuInfo.position);
			intent
					.putExtra("com.emuporium.gymrat.android.RoutineId", r
							.getId());
			intent.putExtra("Routine", r);
			intent.putExtra("Source", "edit");
			startActivity(intent);
			return true;
		}

		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == 99)
			onCreate(null);
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.e("ROUTINEMAIN","RESUME");
		if (!db.isOpen())
			db = DbUtils.getStaticDb();
		adapter.open();
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.e("ROUTINEMAIN","PAUSE");
		db.close();
		adapter.close();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.home, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		startActivity(MenuHandler.handleMenu(this, item));
		return super.onOptionsItemSelected(item);
	}

}
